package by.bsu.touragency.tag;

import java.io.IOException;
import java.util.Scanner;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.log4j.Logger;

import by.bsu.touragency.command.guest.ConfigurationManager;
import by.bsu.touragency.db.beans.Person;
import by.bsu.touragency.db.dao.BookedTourDAO;
import by.bsu.touragency.db.dao.PersonDAO;
import by.bsu.touragency.db.dao.TourDAO;
import by.bsu.touragency.notification.EmailSender;

public class CancelTourTag extends BodyTagSupport{
	private static final long serialVersionUID = 1L;
	private static Logger logger=Logger.getLogger("log");
	
	private Person client;
	private String login;
	private int idTour;
	private TourDAO tour = new TourDAO();
	private int id;
	private boolean flag;
	private EmailSender sending;
	
	public void setUser(String user) {
		this.login = new String(user);
	}
	public int doStartTag() throws JspTagException {
		logger.info("Cancel tour tag do start");
		flag = true;
		PersonDAO cd = new  PersonDAO();
		client = cd.readByLogin(login);
		Scanner sc = new Scanner(pageContext.getRequest().getParameter("idTour"));
		idTour = sc.nextInt();
		
		BookedTourDAO bd = new BookedTourDAO();
		id = bd.readByTourClient(client.getId(),idTour).getId();
	    bd.deleteById(id);
		
		return EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspTagException {
		logger.info("Cancel tour tag After body");
		if (flag) {
			try {
				pageContext.getOut().write("<div class=");
				pageContext.getOut().write("\"post\"");
				pageContext.getOut().write(">");
				pageContext.getOut().write("<h1 class=");
				pageContext.getOut().write("\"title\"");
				pageContext.getOut().write(">");
				
				pageContext.getOut().write(login);
				
				pageContext.getOut().write("</h1>");
				pageContext.getOut().write("<div class=");
				pageContext.getOut().write("\"entry\"");
				pageContext.getOut().write(">");
				pageContext.getOut().write("<p><img src=\"");
				pageContext.getOut().write(pageContext.getServletContext().getContextPath()+ 
						ConfigurationManager.getInstance().getProperty(
						ConfigurationManager.IMAGE_PAGE_PATH)+"\"");
				pageContext.getOut().write("alt=\"\" width=");
				pageContext.getOut().write("\"140\"");
				pageContext.getOut().write("height=");
				pageContext.getOut().write("\"125\"");
				pageContext.getOut().write("class=");
				pageContext.getOut().write("\"left\"");
				pageContext.getOut().write("/>");
				
				pageContext.getOut().write("Cancel tour at TourAgency!");
				
				pageContext.getOut().write("</p>");
				pageContext.getOut().write("<p>&nbsp;</p>");
				pageContext.getOut().write("</div>");
				pageContext.getOut().write("</div>");
				
			    flag = false;
			    
			    String body = "Cancel tour at TourAgency!";
				body+="\n"+ tour.readById(idTour).getHotel().getName()+" "+tour.readById(idTour).getHotel().getStar().getName();
				body+="\n"+tour.readById(idTour).getHotel().getCity().getCityName()+", "+tour.readById(idTour).getHotel().getCity().getCountry().getCountryName();
				body+="\n"+tour.readById(idTour).getPrice()+"$";
		        setSending(new EmailSender(client.getEmail(),client.getLogin(), body));
				
			} catch (IOException e) {
				logger.error("Error in cancel_tag");
			}
			return EVAL_BODY_AGAIN;
		} else {
			return SKIP_BODY;
		}
	}

	public void setSending(EmailSender sending) {
		this.sending = sending;
	}
	public EmailSender getSending() {
		return sending;
	}
	public int doEndTag() throws JspTagException {
		return SKIP_BODY;
	}
}
